package com.bbt.dormassistant.controller;

import com.bbt.dormassistant.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Api(tags = "文章接口")
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;


    @ApiOperation(value = "根据userID获取该用户的所有文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String")
    })
    @GetMapping("/getMessageByUserId")
    public Map<String,Object> getMessageByUserId(@RequestParam("userId") String userId){
        Map<String,Object> map = new HashMap<>();
        map.put("messages",messageService.getAllMessageByUserId(userId));
        return map;
    }


    @ApiOperation(value = "获取所有文章信息")
    @GetMapping("/getAllMessage")
    public Map<String,Object> getAllMessage(){
        Map<String,Object> result = new HashMap<>();
        result.put("messages",messageService.getAllMessage());
        return result;
    }


    @ApiOperation(value = "获取所有文章简略（标题）信息")
    @GetMapping("/getAllMessageTitles")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currIndex", value = "当前页码(可选项)", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页大小(可选项)",  dataType = "int")
    })
    public Map<String,Object> getAllMessageTitles(@RequestParam(value = "currIndex",required = false) Integer currIndex,
                                                  @RequestParam(value = "pageSize",required = false) Integer pageSize){
        Map<String,Object> result = new HashMap<>();
        result.put("messages",messageService.getAllMessageTitles(currIndex,pageSize));
        return result;
    }


    @ApiOperation(value = "添加一篇新的文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题", required = true, dataType = "String"),
            @ApiImplicitParam(name = "text", value = "内容", required = true, dataType = "String")
    })
    @PostMapping(value = "/addMessage")
    public Map<String,Object> addMessage(@RequestParam(value = "userId") String userId,
                                         @RequestParam(value = "title") String title,
                                         @RequestParam(value = "text") String text
                                         ){
        Map<String,Object> map = new HashMap<>();
        map.put("success",messageService.addMessage(userId,title,text));
        return map;
    }

    @ApiOperation("上传图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "image", value = "图片", dataType = "file")
    })
    @PostMapping(value = "/uploadImage",consumes = {"multipart/*"},headers = "content-type=multipart/form-data")
    public Map<String,Object> uploadImage(@RequestParam("image") MultipartFile image, @RequestParam(value = "user") String user) throws IOException {
        String imageUrl=messageService.addImage(image);
        Map<String,Object> res=new HashMap<>();
        res.put("imageUrl",imageUrl);
        return res;
    }

    @ApiOperation(value = "根据messageID获取完整的文章信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "messageId", value = "文章ID", required = true, dataType = "int")
    })
    @GetMapping("/getMessageByMessageId")
    public Map<String,Object> getMessageByMessageId(@RequestParam("messageId") Integer messageId){
        Map<String,Object> result = new HashMap<>();
        result.put("message",messageService.getMessageByMessageId(messageId));
        return result;
    }

}
